#!/bin/bash

function info() {
  echo -e "\033[32m$1\033[0m"
}

function error() {
  echo -e "\033[31m$1\033[0m"
}

info "内核参数优化"
cat >/etc/sysctl.d/100-user.conf <<'EOF'
fs.file-max=1048576
fs.nr_open=1048576
net.core.somaxconn=32768
net.core.netdev_max_backlog=16384
net.ipv4.tcp_max_syn_backlog=16384
vm.swappiness = 10
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
vm.overcommit_memory = 1
EOF
service procps force-reload
cat > /etc/systemd/user.conf <<'EOF'
DefaultLimitNOFILE=1048576
EOF
systemctl daemon-reload
info "Done!"

info "文件描述符限制优化"
cat >/etc/security/limits.d/100-user.conf <<'EOF'
* soft nofile 65535
* hard nofile 65535
EOF
info "Done!"

info "创建占位文件"
rm -rf /placeholder
dd if=/dev/zero of=/placeholder bs=1M count=2048
info "Done!"

info "限制 Journal 日志占用"
sed -i "s/#SystemMaxUse.*/SystemMaxUse=500M/g" /etc/systemd/journald.conf
systemctl restart systemd-journald
info "Done!"

info "配置时间同步"
timedatectl set-timezone Asia/Shanghai
cat >/etc/systemd/timesyncd.conf <<'EOF'
[Time]
NTP=ntp.aliyun.com
FallbackNTP=ntp1.aliyun.com,ntp2.aliyun.com,ntp3.aliyun.com,ntp4.aliyun.com,ntp5.aliyun.com,ntp6.aliyun.com,ntp7.aliyun.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048
EOF
systemctl restart systemd-timesyncd.service
info "Done!"

info "使用中科大镜像源"
cat > /etc/apt/sources.list <<'EOF'
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu/ noble main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置，如有需要可自行修改注释切换
deb https://mirrors.ustc.edu.cn/ubuntu/ noble-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-security main restricted universe multiverse

# deb http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
# # deb-src http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse

# 预发布软件源，不建议启用
# deb https://mirrors.ustc.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
# # deb-src https://mirrors.ustc.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
EOF
info "Done!"

info "更新软件"
apt update
apt upgrade -y
info "Done!"

info "安装常用工具"
apt install -y vim git curl wget tmux htop net-tools zsh lrzsz zip unzip tree
info "Done!"

info "vim 优化"
cat >~/.vimrc <<'EOF'
" 显示标尺
set ruler

" 输入的命令显示出来，看的清楚些
set showcmd

" 状态行显示的内容
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}

" 启动显示状态行1，总是显示状态行2
set laststatus=2

" 语法高亮显示
syntax on

" 编码
set fileencodings=utf-8,gb2312,gbk,cp936,latin-1
set fileencoding=utf-8
set termencoding=utf-8
set fileformat=unix
set encoding=utf-8

" 去掉有关vi一致性模式，避免以前版本的一些bug和局限，解决backspace不能使用的问题
set nocompatible
set backspace=indent,eol,start
set backspace=2

" 启用自动对齐功能，把上一行的对齐格式应用到下一行
set autoindent

" 依据上面的格式，智能的选择对齐方式，对于类似C语言编写很有用处
set smartindent

" vim禁用自动备份
set nobackup
set nowritebackup
set noswapfile

" 用空格代替tab
set expandtab

" 设置显示制表符的空格字符个数,改进tab缩进值，默认为8，现改为4
set tabstop=4
" 统一缩进为4，方便在开启了et后使用退格(backspace)键，每次退格将删除X个空格
set softtabstop=4
" 设定自动缩进为4个字符，程序中自动缩进所使用的空白长度
set shiftwidth=4

" 显示匹配的括号
set showmatch

" 文件缩进及tab个数
au FileType html,python,vim,javascript setl shiftwidth=4
au FileType html,python,vim,javascript setl tabstop=4
au FileType java,php setl shiftwidth=4
au FileType java,php setl tabstop=4

" 高亮搜索的字符串
set hlsearch

" 检测文件的类型
filetype on
filetype plugin on
filetype indent on

" C风格缩进
set cindent
set completeopt=longest,menu

" 自动保存
set autowrite

" 共享剪贴板
set clipboard+=unnamed

" 文件被改动时自动载入
set autoread

" 顶部底部保持3行距离
set scrolloff=3
EOF
info "Done!"

info "安装oh-my-zsh"
git clone https://mirror.nju.edu.cn/git/ohmyzsh.git ~/ohmyzsh
REMOTE=https://mirror.nju.edu.cn/git/ohmyzsh.git sh ~/ohmyzsh/tools/install.sh --unattended
rm -rf ~/ohmyzsh
info "Done!"

info "安装oh-my-zsh插件: zsh-autosuggestions"
git clone https://mirror.nju.edu.cn/git/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

info "安装oh-my-zsh插件: zsh-syntax-highlighting"
git clone https://mirror.nju.edu.cn/git/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

info "安装oh-my-zsh插件: zsh-autocomplete"
git clone https://mirror.nju.edu.cn/git/zsh-autocomplete.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autocomplete
info "Done!"

info "配置 .zshrc"
cat > ~/.zshrc <<'EOF'
# 设置 Oh My Zsh 的安装路径
export ZSH="$HOME/.oh-my-zsh"

# 禁用自动更新 Oh My Zsh
zstyle ':omz:update' mode disabled

# 设置主题
ZSH_THEME="robbyrussell"

# 设置历史记录时间戳格式
HIST_STAMPS="yyyy-mm-dd"

# 禁用魔术函数
DISABLE_MAGIC_FUNCTIONS="true"

# 加载插件
plugins=(
  z
  git
  sudo
  common-aliases
  zsh-autosuggestions
  zsh-syntax-highlighting
  zsh-autocomplete
)

# Oh My Zsh 的初始化脚本
source $ZSH/oh-my-zsh.sh
EOF
info "Done!"

info "清理软件"
apt autoclean -y
apt autoremove -y
info "Done!"

info "清空历史"
history -c
info "Done!"

info "切换shell为zsh"
chsh -s $(which zsh)
info "Done!请重启后生效"
